$(function () {
    var layer = layui.layer
    var id = location.search.slice(4)
    // console.log(location);
    // console.log(location.href);
    // console.log(location.search);
    // console.log(id);
    // 数据回填
    $.ajax({
        method: 'GET',
        url: 'admin/users/' + id,
        success: function (res) {
            // console.log(res);
            if (res.status !== 0) {
                return layer.msg(res.message)
            }
            //成功的话就把res.data里的数据填到对应的输入框里
            // $('[name = id]').val(res.data.id)
            $('[name = username]').val(res.data.username)
            $('[name = nickname]').val(res.data.nickname)
            $('[name = email]').val(res.data.email)
        }
    })

    //编辑
    $('body').on('submit', '#userEdit', function (e) {
        e.preventDefault()
        //发送ajax请求
        $.ajax({
            method: 'PUT',
            url: 'admin/users',
            data: {
                //不能用serialize()因为serialize获取的表单数据都是字符串型,
                //而传入的参数id要是谁类型
                id: parseInt(id),
                username: $('[name=username]').val(),
                nickname: $('[name=nickname]').val(),
                email: $('[name=email]').val()
            },
            success: function (res) {
                if (res.status !== 0) {
                    return layer.msg('编辑用户失败!')
                }
                layer.msg('编辑用户成功')
                location.href = "/user/user.html"
            }
        })
    })
})